package com.example.commontest.service.Impl;

import com.example.commontest.service.AsyncService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;

/**
 * @Author huaiyu yin
 * @Date 2021/5/24 11:52 上午
 * @Version 1.0
 */
@Service
public class AsyncServiceImpl implements AsyncService {


    @Override
    @Async("asyncExecutor")
    public Future<Integer> getFollowed(Integer times, CountDownLatch latch) {
        Integer count = 0;
        //负责相关使用逻辑
        for (int i = 0; i < times; i++) {
            count += i;
        }
        latch.countDown();
        return new AsyncResult<>(new Integer(count));
    }

    @Override
    @Async("asyncExecutor")
    public Future<Integer> getAtention(Integer times, CountDownLatch latch) {
        Integer count = 0;
        //负责相关使用逻辑
        for (int i = 0; i < times; i++) {
            count += i;
        }
        latch.countDown();
        return new AsyncResult<>(new Integer(count));
    }
}
